package com.zhixiang.xaec.action.performance.safeachievements;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.zhixiang.core.command.QueryFilter;
import com.zhixiang.core.entity.TreeObj;
import com.zhixiang.core.util.JsonUtil;
import com.zhixiang.core.web.action.BaseAction;
import com.zhixiang.xaec.entity.performance.safeachievements.RatingProjectSetTree;
import com.zhixiang.xaec.service.performance.safeachievements.RatingProjectSetTreeService;

@Controller
@Scope("prototype")
public class RatingProjectSetTreeAction extends BaseAction {

    private static final long serialVersionUID = -7374192170833329500L;

    private RatingProjectSetTree ratingProjectSetTree;

    @Autowired
    private RatingProjectSetTreeService ratingProjectSetTreeService;

    public String save() {
        ratingProjectSetTreeService.saveRatingProject(ratingProjectSetTree);
        return SUCCESS;
    }

    public String get() {
        if (ratingProjectSetTree != null && StringUtils.isNotEmpty(ratingProjectSetTree.getId().toString())) {
            setResult(ratingProjectSetTreeService.get(ratingProjectSetTree.getId()));
        }
        return SUCCESS;
    }

    public String list() {
        QueryFilter localQueryFilter = new QueryFilter(getRequest());
        List<RatingProjectSetTree> list = ratingProjectSetTreeService.getAll(localQueryFilter);
        setTotalCounts(localQueryFilter.getPagingBean().getTotalItems());
        setResult(list);
        return SUCCESS;
    }

    public void getAll() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("Q_parentId_L_EQ", "1");
        List<TreeObj> list = ratingProjectSetTreeService.getTreeObj(1L, "internalName", map);
        // List<TreeObj> treelist = new ArrayList<TreeObj>();
        // for (Department t : list) {
        // TreeObj obj = new TreeObj();
        // obj.setId(t.getDepId().toString());
        // obj.setText(t.getDepName());
        // obj.setLeaf(true);
        // treelist.add(obj);
        // }
        // 直接向页面输出字符串
        outJsonString(JsonUtil.object2Json(list));
        // outJsonString(new Gson().toJson(list));
    }

    public String delete() {
        try {
            String id = getRequest().getParameter("id");
            ratingProjectSetTreeService.remove(new Long[] { Long.valueOf(id) });
        } catch (Exception e) {
            setSuccess(false);
            setMsg("删除失败");
        }
        return SUCCESS;
    }

    public RatingProjectSetTree getRatingProjectSetTree() {
        return ratingProjectSetTree;
    }

    public void setRatingProjectSetTree(RatingProjectSetTree ratingProjectSetTree) {
        this.ratingProjectSetTree = ratingProjectSetTree;
    }

}
